﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using com.paypal.sdk.util;
using BLL;
using Models;
using System.Xml;
using com.paypal.sdk.services;
using System.Data;
using Web.Old_App_Code;


public partial class paypal_reviewByPaypal : System.Web.UI.Page
{
    // Fields
    protected GroupBuyOrderDetailManager gbdom = new GroupBuyOrderDetailManager();
    protected GroupBuyOrderManager gbom = new GroupBuyOrderManager();
    protected GoodsEntitysSpecificationsManager gesm = new GoodsEntitysSpecificationsManager();
    protected OrderGoodsEntitysManager ogem = new OrderGoodsEntitysManager();
    protected OrderManager om = new OrderManager();

    // Methods
    protected void Page_Load(object sender, EventArgs e)
    {
        int num6;
        int num7;
        string str6;
        DataTable orderInfoByOrderNo = new DataTable();
        DataTable orderGoodsEntitysByOrderNo = new DataTable();
        if (((base.Request.QueryString["orderNo"] == null) && (base.Request.QueryString["alimoney"] == null)) && (this.Session["user"] == null))
        {
            return;
        }
        if ((base.Request.QueryString["orderNo"] != null) && (this.Session["user"] != null))
        {
            if (base.Request.QueryString["orderNo"].Contains("PM_"))
            {
                orderInfoByOrderNo = this.om.GetOrderInfoByOrderNo(base.Request.QueryString["orderNo"]);
                orderGoodsEntitysByOrderNo = this.ogem.GetOrderGoodsEntitysByOrderNo(base.Request.QueryString["orderNo"]);
            }
            else if (base.Request.QueryString["orderNo"].Contains("GB_"))
            {
                orderInfoByOrderNo = this.gbom.GetGroupBuyOrderInfoByOrderNo(base.Request.QueryString["orderNo"]);
                orderGoodsEntitysByOrderNo = this.gbdom.GetGroupBuyOrderDetailByOrderNo(base.Request.QueryString["orderNo"]);
            }
        }
        PaypalConfig config = new PaypalConfig();
        if (config.user == "yuanxindesign_api1.126.com")
        {
            Constants.ENVIRONMENT = "sandbox";
        }
        else
        {
            Constants.ENVIRONMENT = "live";
        }
        Constants.API_USERNAME = config.user;
        Constants.API_PASSWORD = config.pass;
        Constants.API_SIGNATURE = config.sign;
        this.Session["stage"] = Constants.ENVIRONMENT;
        SetProfile.SessionProfile = SetProfile.CreateAPIProfile(Constants.API_USERNAME, Constants.API_PASSWORD, Constants.API_SIGNATURE, "", "", Constants.ENVIRONMENT, "", "", "", "");
        string str = string.Concat(new object[] { base.Request.Url.Scheme, "://", base.Request.Url.Host, ":", base.Request.Url.Port });
        string str2 = "";
        if (base.Request.QueryString["orderNo"] == null)
        {
            str2 = str + "/cn/member_center/online_pay.html";
        }
        else if (base.Request.QueryString["orderNo"].Contains("PM_"))
        {
            str2 = str + "/cn/order_result.html?orderNo=" + base.Request.QueryString["orderNo"];
        }
        else if (base.Request.QueryString["orderNo"].Contains("GB_"))
        {
            str2 = str + "/cn/group_buy_order_result.html?orderNo=" + base.Request.QueryString["orderNo"];
        }
        NVPCallerServices services = PayPalAPI.PayPalAPIInitialize();
        NVPCodec codec = new NVPCodec();
        codec["METHOD"] = "SetExpressCheckout";
        codec["CANCELURL"] = str2;
        codec["PAYMENTACTION"] = "Sale";
        codec["CURRENCYCODE"] = "USD";
        codec["NOSHIPPING"] = "1";
        int num = 0;
        int num2 = 0;
        double num3 = 0.0;
        if ((base.Request.QueryString["orderNo"] != null) && (this.Session["user"] != null))
        {
            int num4;
            DataRow row;
            if (base.Request.QueryString["orderNo"].Contains("PM_"))
            {
                for (num4 = 0; num4 < orderGoodsEntitysByOrderNo.Rows.Count; num4++)
                {
                    row = orderGoodsEntitysByOrderNo.Rows[num4];
                    DataTable table3 = this.gesm.FrontDeskGetGoodsEntitysSpecificationsByGoodsEntitys(Convert.ToInt32(row["goodsEntitys"]));
                    string str3 = row["goodsName"].ToString();
                    for (int i = 0; i < table3.Rows.Count; i++)
                    {
                        string str11 = str3;
                        str3 = str11 + ((i > 0) ? " " : "") + table3.Rows[i]["specificationsName"].ToString() + "：" + table3.Rows[i]["specificationsValueName"].ToString();
                    }
                    codec["L_NAME" + num4] = row["goodsName"].ToString();
                    codec["L_NUMBER" + num4] = row["goodsArtNo"].ToString();
                    codec["L_DESC" + num4] = str3;
                    codec["L_AMT" + num4] = Convert.ToDecimal(row["price"]).ToString();
                    codec["L_QTY" + num4] = row["amount"].ToString();
                    num6 = 0;
                    num7 = (int)row["amount"];
                    num2 += num7;
                    num6 = num7;
                    num += num6;
                    num3 += Convert.ToDouble(row["price"]) * num6;
                }
            }
            else if (base.Request.QueryString["orderNo"].Contains("GB_"))
            {
                for (num4 = 0; num4 < orderGoodsEntitysByOrderNo.Rows.Count; num4++)
                {
                    row = orderGoodsEntitysByOrderNo.Rows[num4];
                    codec["L_NAME" + num4] = row["groupBuySummarized"].ToString();
                    codec["L_NUMBER" + num4] = row["gid"].ToString();
                    codec["L_DESC" + num4] = row["groupBuySummarized"].ToString();
                    codec["L_AMT" + num4] = Convert.ToDecimal(row["price"]).ToString();
                    codec["L_QTY" + num4] = row["amount"].ToString();
                    num6 = 0;
                    num7 = (int)row["amount"];
                    num2 += num7;
                    num6 = num7;
                    num += num6;
                    num3 += Convert.ToDouble(row["price"]) * num6;
                }
            }
        }
        else if ((base.Request.QueryString["alimoney"] != null) && (this.Session["user"] != null))
        {
            codec["L_NAME" + 0] = "Online prepaid phone";
            codec["L_NUMBER" + 0] = DateTime.Now.ToString("yyyyMMddHHmmssffffff");
            codec["L_DESC" + 0] = "Online prepaid phone";
            codec["L_AMT" + 0] = Convert.ToDecimal(base.Request.QueryString["alimoney"]).ToString();
            codec["L_QTY" + 0] = "1";
            num6 = 0;
            num7 = 1;
            num2 += num7;
            num6 = num7;
            num += num6;
            num3 += Convert.ToDouble(base.Request.QueryString["alimoney"]) * num6;
        }
        codec["ITEMAMT"] = num3.ToString();
        double num8 = num3;
        double num9 = Math.Round((double)(num8 + 0.0), 2);
        if ((base.Request.QueryString["orderNo"] != null) && (this.Session["user"] != null))
        {
            codec["SHIPDISCAMT"] = orderInfoByOrderNo.Rows[0]["deliveryMoney"].ToString();
        }
        else if ((base.Request.QueryString["alimoney"] != null) && (this.Session["user"] != null))
        {
            codec["SHIPDISCAMT"] = "0";
        }
        codec["AMT"] = (num8 + Convert.ToDouble(codec["SHIPDISCAMT"])).ToString();
        string str4 = "";
        if ((base.Request.QueryString["orderNo"] != null) && (this.Session["user"] != null))
        {
            if (base.Request.QueryString["orderNo"].Contains("PM_"))
            {
                str4 = string.Concat(new object[] { str, base.ResolveUrl("/paypal/DoExpressCheckoutByPaypal.aspx"), "?currency=USD&paymentType=Sale&amount=", orderInfoByOrderNo.Rows[0]["orderTotal"], "&OrderID=", base.Request.QueryString["orderNo"], "&pt=1" });
            }
            else if (base.Request.QueryString["orderNo"].Contains("PM_"))
            {
                str4 = string.Concat(new object[] { str, base.ResolveUrl("/paypal/DoExpressCheckoutByPaypal.aspx"), "?currency=USD&paymentType=Sale&amount=", orderInfoByOrderNo.Rows[0]["orderTotal"], "&OrderID=", base.Request.QueryString["orderNo"], "&pt=2" });
            }
        }
        else
        {
            str4 = str + base.ResolveUrl("/paypal/DoExpressCheckoutByPaypal.aspx") + "?currency=USD&paymentType=Sale&amount=" + codec["L_AMT" + 0] + "&OrderID=" + codec["L_NUMBER" + 0] + "&pt=0";
        }
        codec["RETURNURL"] = str4;
        codec["MAXAMT"] = num9.ToString();
        codec["INSURANCEOPTIONOFFERED"] = "true";
        string requestnvp = codec.Encode();
        int num10 = 10;
        int num11 = 0;
    Label_0C34: ;
        try
        {
            str6 = services.Call(requestnvp);
        }
        catch (Exception exception)
        {
            if (num11 < num10)
            {
                num11++;
                goto Label_0C34;
            }
            base.Response.Write(exception.Message);
            base.Response.Flush();
            base.Response.End();
            return;
        }
        NVPCodec codec2 = new NVPCodec();
        codec2.Decode(str6);
        string str7 = codec2["ACK"];
        if ((str7 != null) && ((str7 == "Success") || (str7 == "SuccessWithWarning")))
        {
            this.Session["TOKEN"] = codec2["TOKEN"];
            string str8 = "www." + this.Session["stage"].ToString() + ".paypal.com";
            string url = "https://" + str8 + "/cgi-bin/webscr?cmd=_express-checkout&token=" + codec2["TOKEN"];
            base.Response.Redirect(url);
        }
        else
        {
            this.Session["errorresult"] = codec2;
            string str10 = "API=Error Detail ";
            base.Response.Redirect("APIError.aspx?" + str10);
        }
    }

}